#include "../include/gudb.h"
#include <stdio.h>
#include <stdlib.h>

int main() {
    printf("=== GuDB 基础操作教程 ===\n\n");
    
    // 1. 打开/创建数据库
    printf("1. 打开数据库...\n");
    gudb* db = gudb_open("/tmp/tutorial_db", GU_OPEN_CREATE, "admin", "admin");
    if (!db) {
        printf("❌ 数据库打开失败\n");
        return -1;
    }
    printf("✅ 数据库打开成功\n\n");
    
    // 2. 开始事务
    printf("2. 开始事务...\n");
    gudb_txn* txn = gudb_begin(db, GU_TXN_RC);
    if (!txn) {
        printf("❌ 事务开始失败\n");
        gudb_close(db);
        return -1;
    }
    printf("✅ 事务开始成功\n\n");
    
    // 3. 创建表
    printf("3. 创建用户表...\n");
    result_t* result = NULL;
    int ret = gudb_exec(txn, 
        "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(50), age INT, email VARCHAR(100))", 
        NULL, &result);
    
    if (ret == GU_OK) {
        printf("✅ 表创建成功\n");
    } else {
        printf("⚠️  表可能已存在，继续...\n");
    }
    if (result) {
        gudb_free_result(result);
        result = NULL;
    }
    
    // 4. 插入数据
    printf("\n4. 插入用户数据...\n");
    const char* insert_queries[] = {
        "INSERT INTO users VALUES (1, 'Alice', 25, 'alice@example.com')",
        "INSERT INTO users VALUES (2, 'Bob', 30, 'bob@example.com')",
        "INSERT INTO users VALUES (3, 'Charlie', 35, 'charlie@example.com')",
        "INSERT INTO users VALUES (4, 'Diana', 28, 'diana@example.com')"
    };
    
    for (int i = 0; i < 4; i++) {
        ret = gudb_exec(txn, insert_queries[i], NULL, &result);
        if (ret == GU_OK) {
            printf("✅ 用户 %d 插入成功\n", i + 1);
        } else {
            printf("⚠️  用户 %d 插入失败或已存在\n", i + 1);
        }
        if (result) {
            gudb_free_result(result);
            result = NULL;
        }
    }
    
    // 5. 查询数据
    printf("\n5. 查询用户数据...\n");
    ret = gudb_exec(txn, "SELECT * FROM users ORDER BY age", NULL, &result);
    
    if (ret == GU_OK && result && result->rows) {
        printf("📊 查询结果 (%u 行):\n", result->row_count);
        printf("%-5s %-10s %-5s %-20s\n", "ID", "Name", "Age", "Email");
        printf("------------------------------------------\n");
        
        for (uint32_t i = 0; i < result->row_count; i++) {
            printf("%-5s %-10s %-5s %-20s\n", 
                   result->rows[i][0] ? result->rows[i][0] : "NULL",
                   result->rows[i][1] ? result->rows[i][1] : "NULL",
                   result->rows[i][2] ? result->rows[i][2] : "NULL",
                   result->rows[i][3] ? result->rows[i][3] : "NULL");
        }
    } else {
        printf("❌ 查询失败\n");
    }
    
    // 6. 条件查询
    printf("\n6. 条件查询（年龄 >= 30）...\n");
    if (result) {
        gudb_free_result(result);
        result = NULL;
    }
    
    ret = gudb_exec(txn, "SELECT name, age FROM users WHERE age >= 30", NULL, &result);
    if (ret == GU_OK && result && result->rows) {
        printf("📊 筛选结果 (%u 行):\n", result->row_count);
        for (uint32_t i = 0; i < result->row_count; i++) {
            printf("  %s, 年龄: %s\n", 
                   result->rows[i][0] ? result->rows[i][0] : "NULL",
                   result->rows[i][1] ? result->rows[i][1] : "NULL");
        }
    }
    
    // 7. 更新数据
    printf("\n7. 更新用户邮箱...\n");
    if (result) {
        gudb_free_result(result);
        result = NULL;
    }
    
    ret = gudb_exec(txn, "UPDATE users SET email = 'alice.new@example.com' WHERE name = 'Alice'", NULL, &result);
    if (ret == GU_OK) {
        printf("✅ Alice 的邮箱更新成功\n");
    }
    
    // 8. 提交事务
    printf("\n8. 提交事务...\n");
    if (gudb_commit(txn) == GU_OK) {
        printf("✅ 事务提交成功\n");
    } else {
        printf("❌ 事务提交失败\n");
    }
    
    // 9. 清理资源
    if (result) {
        gudb_free_result(result);
    }
    gudb_close(db);
    
    printf("\n🎉 基础操作教程完成！\n");
    printf("数据库文件保存在: /tmp/tutorial_db\n");
    printf("\n要编译此程序，请使用:\n");
    printf("gcc tutorial_basic.c -I../include -L.. -lgudb -lpthread -o tutorial_basic\n");
    
    return 0;
}